/*
 * @Author: wuhao930406 1148547900@qq.com
 * @Date: 2023-09-11 08:46:34
 * @LastEditors: wuhao930406 1148547900@qq.com
 * @LastEditTime: 2023-09-27 15:08:16
 * @FilePath: /utools_web/src/app.jsx
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import { history } from '@umijs/max';
import { getFetch } from './utils/doFetch';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import { message } from 'antd';

dayjs.locale('zh-cn');

const loginPath = '/user/login';
const theme = localStorage.getItem('THEME');
/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
export async function getInitialState() {
  //当前主题判断
  const curtheme = theme && theme !== 'undefined' ? theme : 'light';

  const orgconfig = {
    type: 'personal', // 枚举值 personal-个人使用  team-团队使用
    modules: ['report', 'auditreport', 'message', 'scheduled', 'worktime', 'cloud', 'onlineChat'], // 枚举值 report-工时流程  auditreport-工时审批 message-消息推送 scheduled-定时生成任务 worktime-工时登记 cloud-云存储  onlineChat-在线沟通

  };

  const fetchUserInfo = async () => {
    try {
      const msg = await getFetch({
        url: '/system/user',
      });
      localStorage.setItem('THEME', msg?.data?.data?.theme);
      if (msg.data?.data?.role === '3') {
        message.error('普通用户无法登录后台系统')
        setTimeout(() => {
          history.replace(loginPath);
        }, 10);
      }

      return msg.data.data;
    } catch (error) {
    }
    return undefined;
  };

  const fetchProject = async (params = {}) => {
    try {
      const res = await getFetch({
        url: '/project',
        params,
      });
      return res.data.dataList;
    } catch (error) {
    }
    return undefined;
  };

  //如果不是登录页面，执行
  const { location } = history;

  const defaultState = {
    fetchUserInfo,
    fetchProject,
    proList: [],//项目列表 根据条件筛选 工作台
    myProList: [],//我的项目列表
    drawerAll: {},
    curpro: {
      id: null,
    },
    activeUserIdList: [],
    updateSelect: '11',
    orgconfig,
    viewType: "step",
    selectedDate: dayjs(),
    kanbansparams: {
      projectTagIds: [],
      userIds: '',
      itemStatus: ''
    },
    unreadJoinOrgInvitationCount: 0,
    range: [dayjs().startOf('week'), dayjs().endOf('week')]
  };


  if (location.pathname !== loginPath) {
    const currentUser = await fetchUserInfo();
    return {
      ...defaultState,
      currentUser: {
        ...(currentUser ?? {}),
        theme: currentUser?.theme ?? curtheme,
        waringTime: 1,
        waringType: localStorage.getItem('waringType')
          ? JSON.parse(localStorage.getItem('waringType'))
          : null,
        waringfilter: localStorage.getItem('waringfilter')
          ? JSON.parse(localStorage.getItem('waringfilter'))
          : null,
      },
    };
  }

  return {
    ...defaultState,
    currentUser: {
      theme: curtheme,
      waringTime: 1,
      waringType: localStorage.getItem('waringType')
        ? JSON.parse(localStorage.getItem('waringType'))
        : null,
      waringfilter: localStorage.getItem('waringfilter')
        ? JSON.parse(localStorage.getItem('waringfilter'))
        : null,
    },
  };
}
